< Prev(J) / Next(K) > / View :

Filename: /text/sbasic/shared/03090202.xhp

(section: fornext) (bookmark: bm_id3149205)
For statement
To statement
Step statement
Next statement

For; ステートメント
To; ステートメント
Step; ステートメント
Next; ステートメント

For...Next Statement [Runtime]
For...Next ステートメント [実行時](hd_id3149205.1)

Repeats the statements between the For...Next block a specified number of times.
For...Next ブロックの間にあるステートメントを、指定回数分だけ繰り返し実行させます。(par_id3143267.2)

(/section: fornext)

Syntax:
構文:(hd_id3156153.3)

For counter=start To end [Step step]
For counter=start To end [Step step](par_id3148473.4)

statement block
ステートメントブロック(par_id3156024.5)

[Exit For]
[Exit For](par_id3146796.6)

statement block
ステートメントブロック(par_id3159414.7)

Next [counter]
Next [counter](par_id3153897.8)

Variables:
変数:(hd_id3150400.9)

Counter: Loop counter initially assigned the value to the right of the equal sign (start). Only numeric variables are valid. The loop counter increases or decreases according to the variable Step until End is passed.
Counter: ループカウンタとして用いる変数で、イコール記号の右側の値 (start) が初期値として割り当てられます。使用可能なものは、数値変数だけです。ループカウンタの値は、Step の指定値に応じて加算ないし減算が行われ、End の指定値に到達するかがチェックされます。(par_id3150358.10)

Start: Numeric variable that defines the initial value at the beginning of the loop.
Start: ループ開始時における初期値を指定する数値変数。(par_id3152455.11)

End: Numeric variable that defines the final value at the end of the loop.
End: ループ終了時における終了値を指定する数値変数。(par_id3151043.12)

Step: Sets the value by which to increase or decrease the loop counter. If Step is not specified, the loop counter is incremented by 1. In this case, End must be greater than Start. If you want to decrease Counter, End must be less than Start, and Step must be assigned a negative value.
Step: ループカウンタの加算幅ないし減算幅を指定する増分値。Step に値を指定しないと、ループカウンタの増分値は 1 とされます。この場合は、Start の指定値よりも大きな値を、End に指定する必要があります。ループカウンタの値を減算させる場合は、Step に負の値を指定して、Start の指定値よりも小さな値を End に指定する必要があります。(par_id3156281.13)

The For...Next loop repeats all of the statements in the loop for the number of times that is specified by the parameters.
For...Next ループでは、パラメータ群で指定する回数分だけ、ステートメントブロックの全コードが繰り返されます。(par_id3154684.14)

As the counter variable is decreased, Office Basic checks if the end value has been reached. As soon as the counter passes the end value, the loop automatically ends.
ループカウンタの値が変化すると、終了値に到達したかを Office Basic がチェックします。カウンタ値が終了値に到達すると、ループは終了します。(par_id3147287.15)

It is possible to nest For...Next statements. If you do not specify a variable following the Next statement, Next automatically refers to the most recent For statement.
For...Next ステートメントは、複数のものをネスト (入れ子化) することができます。Next ステートメントに続けて変数名を指定しない場合、各 Next は一番近くに存在する For ステートメントに対応づけられます。(par_id3159154.16)

If you specify an increment of 0, the statements between For and Next are repeated continuously.
増分値を0とすると、For から Next までの間にあるステートメントブロックが永続的に繰り返し実行され続けます。(par_id3155306.17)

When counting down the counter variable, Office Basic checks for overflow or underflow. The loop ends when Counter exceeds End (positive Step value) or is less than End (negative Step value).
カウンタ変数値の増減に対しては、オーバーフローまたはアンダーフローの発生を Office Basic がチェックします。 カウンタ変数の値が End の指定値を超過するか (Step に正の値を指定した場合)、End の指定値を下回った (Step に負の値を指定した場合) 段階で、ループは終了します。(par_id3155854.18)

Use the Exit For statement to exit the loop unconditionally. This statement must be within a For...Next loop. Use the If...Then statement to test the exit condition as follows:
Exit For ステートメントを指定すると、ループは無条件に終了させられます。このステートメントは、For...Next ループの内部に記述する必要があります。この場合も、下記の例のように If...Then ステートメントを使って、終了条件を満たしているかの判定が行えます。(par_id3145273.19)

For...
For...(par_id3153190.20)

statements
ステートメント群(par_id3149482.21)

If condition = True Then Exit For
If 終了条件 = True Then Exit For(par_id3147124.22)

statements
ステートメント群(par_id3153159.23)

Next
Next(par_id3154096.24)

Note: In nested For...Next loops, if you exit a loop unconditionally with Exit For, only one loop is exited.
注意:複数の For...Next ループをネストしている場合、1 つの Exit For ステートメントで無条件に終了できるループは 1 つだけです。(par_id3156286.25)

Example
(hd_id3148457.26)

The following example uses two nested loops to sort a string array with 10 elements ( sEntry() ), that are first filled with various contents:
下記の例では、初期設定として各種の名前を代入した要素数 10 の配列 ( sEntry( )) を用意しておき、二重にネストしたループを使って要素の並べ替えを行っています。(par_id3151074.27)

Sub ExampleSort
Sub ExampleSort(par_id3155603.28)

Dim sEntry(9) As String
Dim sEntry(9) As String(par_id3156275.29)

Dim iCount As Integer
Dim iCount As Integer(par_id3155066.30)

Dim iCount2 As Integer
Dim iCount2 As Integer(par_id3150751.31)

Dim sTemp As String
Dim sTemp As String(par_id3155446.32)

sEntry(0) = "Jerry"
sEntry(0) = "Jerry"(par_id3155767.42)

sEntry(1) = "Patty"
sEntry(1) = "Patty"(par_id3153711.33)

sEntry(2) = "Kurt"
sEntry(2) = "Kurt"(par_id3148993.34)

sEntry(3) = "Thomas"
sEntry(3) = "Thomas"(par_id3156382.35)

sEntry(4) = "Michael"
sEntry(4) = "Michael"(par_id3155174.36)

sEntry(5) = "David"
sEntry(5) = "David"(par_id3166448.37)

sEntry(6) = "Cathy"
sEntry(6) = "Cathy"(par_id3149255.38)

sEntry(7) = "Susie"
sEntry(7) = "Susie"(par_id3149565.39)

sEntry(8) = "Edward"
sEntry(8) = "Edward"(par_id3145148.40)

sEntry(9) = "Christine"
sEntry(9) = "Christine"(par_id3145229.41)

For iCount = 0 To 9
For iCount = 0 To 9(par_id3149107.44)

For iCount2 = iCount + 1 To 9
For iCount2 = iCount + 1 To 9(par_id3148485.45)

If sEntry(iCount) > sEntry(iCount2) Then
If sEntry(iCount) > sEntry(iCount2) Then(par_id3155608.46)

sTemp = sEntry(iCount)
sTemp = sEntry(iCount)(par_id3150938.47)

sEntry(iCount) = sEntry(iCount2)
sEntry(iCount) = sEntry(iCount2)(par_id3153790.48)

sEntry(iCount2) = sTemp
sEntry(iCount2) = sTemp(par_id3149210.49)

End If
End If(par_id3153781.50)

Next iCount2
Next iCount2(par_id3158446.51)

Next iCount
Next iCount(par_id3150783.52)

For iCount = 0 To 9
For iCount = 0 To 9(par_id3151278.57)

Print sEntry(iCount)
Print sEntry(iCount)(par_id3148462.58)

Next iCount
Next iCount(par_id3149528.59)

End Sub
End Sub(par_id3152580.60)


< Prev / Next >